//
//  SpringViewController.m
//  ProjectA
//
//  Created by dlios on 15-3-5.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SpringViewController.h"

@interface SpringViewController ()

@end

@implementation SpringViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    //创建UITableView
   self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 49) style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    self.springArray = [NSMutableArray array];
    //------------------解析数据-------------------------------//
    //打开数据库
    [[SeasonDataBase shareSpringDataBase] openDB];
    [[SeasonDataBase shareSpringDataBase] createTable];
    

    [AFNDownLoader getDataDownLoad:@"http://api.yi18.net/cook/list?id=82&limit=20&page=1&type=id" block:^(id myData) {
        NSMutableDictionary *dic = myData;
        NSMutableArray *myArray = [dic objectForKey:@"yi18"];
        
        //先删掉数据库然后创建数据库
        [[SeasonDataBase shareSpringDataBase] deleteTable];
        [[SeasonDataBase shareSpringDataBase] createTable];
        
        for (NSDictionary *myDic in myArray) {
            SpringModel *springModel = [[SpringModel alloc] init];
            springModel.name = [myDic objectForKey:@"name"];
            springModel.food = [myDic objectForKey:@"food"];
            springModel.img = [myDic objectForKey:@"img"];
            springModel.id = [myDic objectForKey:@"id"];
        [[SeasonDataBase shareSpringDataBase] insertSpringModel:springModel];
            [self.springArray addObject:springModel];
            
        }
        [self.tableV reloadData];
        [self.HUD removeFromSuperview];
    }];
    
    //菊花
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
   [self.HUD showAnimated:YES whileExecutingBlock:^{
       self.HUD.labelText = @"Loading";
       [self  showTime];
   } completionBlock:^{
       
       
   }];
    
    
    // Do any additional setup after loading the view.
}
- (void)showTime
{
    sleep(9);
}


//tableView必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[SeasonDataBase shareSpringDataBase] selectSpring].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellMark = @"cellIndex";
     SpringTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMark];
    if (cell == nil) {
       cell = [[SpringTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellMark];
    }
    
    SpringModel *data = [[[SeasonDataBase shareSpringDataBase] selectSpring]  objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.nameLable.text = data.name;
    cell.foodLable.text = data.food;
    //万能公式拼接网络请求的图片
    NSString *urlStr = [NSString stringWithFormat:@"http://www.yi18.net/%@", data.img];
    NSURL *url = [NSURL URLWithString:urlStr];
    UIImage *placeholderImage = [UIImage imageNamed:@"homePage副本.png"];
    [cell.imageV sd_setImageWithURL:url placeholderImage:placeholderImage];
    
    return cell;
}
//cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

//点击cell进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       //判断网络
        Reachability *wifi = [Reachability reachabilityForLocalWiFi];
        Reachability *conn = [Reachability reachabilityForInternetConnection];
        if ([wifi currentReachabilityStatus] == NotReachable || [conn currentReachabilityStatus] == NotReachable) {
            UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"网络不佳, 请稍后重试" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alterView show];
            [self.view addSubview:alterView];
        }else{
            
            
        SpringDetailViewController *detailV = [[SpringDetailViewController alloc] init];
    SpringModel *springModel = [[[SeasonDataBase shareSpringDataBase] selectSpring]  objectAtIndex:indexPath.row];
    detailV.navigationItem.title = springModel.name;
    detailV.id = springModel.id;
            NSLog(@"id%@", springModel.id);
    [self.navigationController pushViewController:detailV animated:YES];
    
    
        }
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
